package com.jshare.action;

import java.io.File;

import java.io.InputStream;
import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.jshare.entity.Detool;
import com.jshare.service.DetoolService;
import com.opensymphony.xwork2.ActionSupport;

@Component("detoolDeal")
@Scope("prototype")
public class DetoolAction extends ActionSupport {
	
	private static final long serialVersionUID = 1L;
	private int id;
	private Detool detool;
	private List<Detool> detools;
	private DetoolService detoolService;
	private String name;
	private File upload;
	private String uploadContendType;
	private String uploadFileName;
	private String savePath;
	private String fileName;
	private int pageSize = 3;
	private int pageNow = 1;
	private int pageCount = 0;
	private int rowCount = 0;
	
	public String addInput() {
		
		return SUCCESS;
		
	}
	
	public String update() {
		
		this.detoolService.update(detool);
		
		return SUCCESS;
		
	}
	
	public String updateInput() {
		
		detool = detoolService.loadById(id);
		
		return SUCCESS;
		
	}
	
	public String delete() {
		
		this.detoolService.delete(id,savePath);
		
		return SUCCESS;
	}
	
	public String showDetail() {
		
		this.detool = this.detoolService.loadById(id);
		
		return SUCCESS;
		
	}
	
	public String query() {
		
		this.detools = this.detoolService.getListByFuzzyName(name);
		
		this.rowCount = detoolService.getRowCount();
		
		return SUCCESS;
	}
	
	public String download() {
		
		this.setFileName(detoolService.loadById(id).getDwUrl());
		
		detoolService.download(id);
		
		return "download";
	}
	
	public String list() {
		
		detools = this.detoolService.getListByPage(pageNow, pageSize);
		
		this.rowCount = detoolService.getRowCount();
		
		this.pageCount = detoolService.getPageCount();
		
		return SUCCESS;
	}
	
	public String add() {
		
		detool.setDwUrl(uploadFileName);
		
		if(detoolService.exists(detool)) {
			
			addFieldError("fileExists", "该文件已经存在，请修改文件名");
			
			return "addError";
			
		}
		
		detoolService.add(detool, upload, savePath, uploadFileName);
		
		return SUCCESS;
	}
	
	public Detool getDetool() {
		return detool;
	}

	public void setDetool(Detool detool) {
		this.detool = detool;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}


	public List<Detool> getDetools() {
		return detools;
	}

	public void setDetools(List<Detool> detools) {
		this.detools = detools;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getUploadContendType() {
		return uploadContendType;
	}

	public void setUploadContendType(String uploadContendType) {
		this.uploadContendType = uploadContendType;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public String getSavePath() {
		return savePath;
	}

	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}
	
	public InputStream getTargetFile() {
        return ServletActionContext.getServletContext()
        .getResourceAsStream(savePath +"\\" + fileName);
    }

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getPageNow() {
		return pageNow;
	}

	public void setPageNow(int pageNow) {
		this.pageNow = pageNow;
	}

	public int getPageCount() {
		return pageCount;
	}

	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}

	public int getRowCount() {
		return rowCount;
	}

	public void setRowCount(int rowCount) {
		this.rowCount = rowCount;
	}
	
	public DetoolService getDetoolService() {
		return detoolService;
	}
	@Resource
	public void setDetoolService(DetoolService detoolService) {
		this.detoolService = detoolService;
	}
	
}
